package com.oa.modules.approvalsec1.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.oa.modules.approvalsec1.entity.ProcessItemEntity;
import com.oa.modules.approvalsec1.entity.ProjectFileEntity;
import com.oa.modules.approvalsec1.entity.ProjectScheduleEntity;
import com.oa.modules.approvalsec1.service.ProjectFileService;
import com.oa.modules.approvalsec1.service.ProjectScheduleService;
import com.oa.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.oa.common.utils.R;
import com.oa.modules.approvalsec1.service.ProcessItemService;


/**
 * 流程项
 *
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2021-12-10 10:45:00
 */
@RestController
@RequestMapping("/processitem")
@Api(tags = "项目流程项相关接口")
public class ProcessItemController extends AbstractController {
    @Autowired
    private ProcessItemService processItemService;
    @Autowired
    private ProjectFileService projectFileService;
    @Autowired
    private ProjectScheduleService projectScheduleService;

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") Integer id) {
        ProcessItemEntity processItem = processItemService.getById(id);
        return R.ok().put("processItem", processItem);
    }

    @GetMapping("/getScheduleList/{id}")
    public R getScheduleList(@PathVariable("id") Integer id) {
        List<ProcessItemEntity> list = processItemService.list(new QueryWrapper<ProcessItemEntity>().eq("process_id", id).eq("is_enabled", 1).orderByAsc("sort"));
        List<Integer> ids = list.stream().map(ProcessItemEntity::getScheduleId).collect(Collectors.toList());
        ArrayList<ProjectScheduleEntity> scheduleList = new ArrayList<>();
        ids.forEach(item -> {
            ProjectScheduleEntity scheduleEntity = projectScheduleService.getById(item);
            scheduleList.add(scheduleEntity);
        });

        ArrayList<ProjectScheduleEntity> waiDel = new ArrayList<>();
        //过滤父级
        scheduleList.forEach(item->{
            ProjectScheduleEntity schedule = projectScheduleService.getById(item.getParentId());
            if(schedule!=null&&schedule.getParentId()!=0){
                waiDel.add(schedule);
            }
        });

        scheduleList.removeAll(waiDel);

        return R.ok().put("data", scheduleList);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
//    @RequiresPermissions("generator:processitem:save")
    public R save(@RequestBody ProcessItemEntity processItem) {
        processItemService.save(processItem);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
//    @RequiresPermissions("generator:processitem:update")
    public R update(@RequestBody ProcessItemEntity processItem) {
        processItemService.updateById(processItem);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    @RequiresPermissions("generator:processitem:delete")
    public R delete(@RequestBody Integer[] ids) {
        processItemService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 微信端查看文件详情
     */
    @GetMapping("getItemFile")
    @ApiOperation(value = "查询")
    public R getItemFile(String projectId, Integer itemId) {
        LambdaQueryWrapper<ProjectFileEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProjectFileEntity::getProjectId, projectId)
                .eq(ProjectFileEntity::getItemId, itemId);
        ProjectFileEntity entity = projectFileService.getOne(lambdaQueryWrapper);
//        if(entity == null){
//            ProjectFileEntity file = new ProjectFileEntity();
//            String name = projectScheduleService.getById(processItemService.getById(itemId).getScheduleId()).getName();
//            file.setItemName(name);
//            return R.ok().put("item",file);
//        }
        return R.ok().put("item", entity);
    }

    /**
     * 排序
     * processId
     * 数组 【{进度id：序号}】
     * @param params
     * @return
     */
    @PostMapping("/updateSort")
    public R updateSort(@RequestBody Map<String,Object> params){
        ArrayList<Map> sorts = (ArrayList) params.get("sorts");
        sorts.forEach(item->{
            UpdateWrapper<ProcessItemEntity> wrapper = new UpdateWrapper<ProcessItemEntity>().eq("process_id", params.get("processId")).eq("schedule_id", item.get("scheduleId")).set("sort", item.get("sort"));
            processItemService.update(wrapper);
        });
        return R.ok();
    }
}
